package cn.simon.controller;

import cn.simon.model.Orders;
import cn.simon.service.OrdersService;
import cn.simon.tool.ConstantTool;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Log
@Controller
@RequestMapping
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    /***
     * 管理员显示
     * @index 页码
     * @param model
     * @return
     */
    @GetMapping(value = "/admin/orderList")
    public String orderList(@RequestParam(value = "index",defaultValue = "1") int index,Model model){

        IPage<Orders> page = new Page<>();
        page.setRecords(ordersService.getOrderAll(new Page<>(index, ConstantTool.ORDERPAGE)));

        model.addAttribute("page",page);
        return "admin/backstage_order";
    }

    @GetMapping("/buyer/modifyState")
    public String modifyState(@RequestParam(value = "orderId") int orderId,
                              @RequestParam(value = "state") int state,
                              Model model,HttpSession session){

        Orders orders = Orders.builder()
                .orderId(orderId)
                .orderState(state)
                .build();
        ordersService.setByState(orders);
        return "redirect:/buyer/orderList";
    }
    /***
     * 用户订单显示
     * @param index
     * @param model
     * @param session
     * @return
     */
    @GetMapping(value = "/buyer/orderList")
    public String buyerOrderList( @RequestParam( value = "index",defaultValue = "1") int index,
                                 Model model,HttpSession session){

        Integer userId = (Integer) session.getAttribute("userId");
        //测试
        userId = 1;

        IPage<Orders> page = new Page<>();
        page.setRecords(ordersService.getUserOrder(new Page<>(index,ConstantTool.ORDERPAGE),userId));
        model.addAttribute("page",page);
        return "user/orderList";
    }

    /***
     * 商家订单界面显示
     * @param index 页码
     * @param state 订单状态
     * @param session
     * @param model
     * @return
     */

    @GetMapping(value = "/seller/orderList")
    public String sellerOrderList(@RequestParam(value = "index",defaultValue = "1") int index,@RequestParam(value = "state",defaultValue = "-1") int state, HttpSession session, Model model){

        Integer sellerId = (Integer) session.getAttribute("sellerId");
        //sellerId 测试
        sellerId = 1;
        IPage<Orders> page = new Page<>();
        page.setRecords(ordersService.getSellerOrder(new Page<>(index, ConstantTool.ORDERPAGE),sellerId,state));

        model.addAttribute("page",page);
        switch (state) {
            case 2:
                return "seller/sellerOrderNoDeliver";
            case 3:
                return "seller/sellerOrderDeliver";
            case 4:
                return "seller/sellerOrderReceiving";
            case 5:
                return "seller/sellerOrderApplyRefund";
            case 6:
                return "seller/sellerOrderRefund";
            case 7:
                return "seller/sellerOrderSucc";
            case 8:
                return "seller/sellerOrderRegectRefund";
        }
        return "seller/sellerOrderList";
    }

    /***
     * 商家订单状态修改
     * @param orderId 订单id
     * @param state 订单状态
     * @param session
     * @return
     */
    @GetMapping("/seller/confirmDeliver")
    public String orderNoDeliver(int orderId,int state,HttpSession session){
        Orders orders = Orders.builder()
                .orderId(orderId)
                .orderState(state)
                .build();
        ordersService.setByState(orders);
        switch (state){
            case 8:return "redirect:/seller/orderList?state=8";
            case 3:return "redirect:/seller/orderList?state=3";
            case 6:return "redirect:/seller/orderList?state=6";
        }
        return "redirect:/seller/orderList?";
    }

}
